package com.zhi.controller;

import com.zhi.config.Constant;
import com.zhi.entity.Dictionary;
import com.zhi.entity.Teacher;
import com.zhi.entity.User;
import com.zhi.service.DictionaryService;
import com.zhi.service.MaxValueService;
import com.zhi.service.TeacherService;
import com.zhi.service.UserService;
import com.zhi.util.MD5Util;
import com.zhi.util.PageUtil;
import com.zhi.util.PubFun;
import com.zhi.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 教师Controller层
 */
@Controller
public class TeacherController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserService userService;

	@Autowired
    TeacherService teacherService;

    @Autowired
    MaxValueService maxValueService;

    /**
     * 教师列表数据
     * @param model
     * @param pageUtil
     * @return
     */
    @RequestMapping("/toTeacher")
    public String toStudent(Model model,PageUtil pageUtil) {
    	
    	//1、初始化分页工具
    	pageUtil = PageUtil.init(pageUtil);
    	Pageable pageable = PageRequest.of(pageUtil.getPageIndex()-1, pageUtil.getSize());
    	
    	//2、查询分页数据
    	Page<Teacher> pageList=teacherService.getEntityListWithCodeName(pageable);
    	List<Teacher> teachers = pageList.getContent();
        
        //3、设置总页数
    	pageUtil.setTotalPages(pageList.getTotalPages());
    	
    	//4、返回前台数据
    	model.addAttribute("pageUtil", pageUtil);
        model.addAttribute("teachers", teachers);
        return "teacher/list";
    }

    /**
     * 增加教师页面
     * @param model
     * @return
     */
    @RequestMapping("/teacher/toAdd")
    public String toAdd(Model model) {
        return "teacher/add";
    }

    /**
     * 增加教师
     * @param teacher
     * @return
     */
    @RequestMapping("/teacher/add")
    @ResponseBody
    public R add(Teacher teacher) {

        //1教师工号：教师标识号+年份+序号，例2001 2016 00001
        //查询code对应的最大值，若无结果取0并更新1，若有结果取n并更新n+1
        Integer maxvalue = maxValueService.getMax(Constant.ID_BEGIN_TEACHER);
        if(maxvalue == null){
            maxvalue = 0;
        }
        maxValueService.saveCodeAndValue(Constant.ID_BEGIN_TEACHER,maxvalue + 1);
        //取得最大值后，补全位数要求
        String teacherIdEnd = PubFun.getStandardChar(maxvalue + 1,5);

        String teacherId = Constant.ID_BEGIN_TEACHER + teacherIdEnd;
        teacher.setTeacherId(teacherId);

        //2保存教师表数据
        teacherService.save(teacher);

        //3获得教师证件号，截取后6位作为初始密码
        String idCardNo = teacher.getIdCardNo();
        String password = idCardNo.substring(idCardNo.length()-6);
        password = MD5Util.md5Encryption(password);//密码MD5加密

        //4新增学生成功后新增对应的user表，为其新增登录用户信息
        //User user = new User(null,teacherId,password,Constant.USER_TYPE_TEACHER,Constant.EFFECTIVE_YES);
        //userService.save(user);

        return R.ok();
    }

    /**
     * 编辑教师页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/teacher/toEdit")
    public String toEdit(Model model,String id) {
        Teacher teacher=teacherService.getOne(id);
        model.addAttribute("teacher", teacher);
        return "teacher/edit";
    }

    /**
     * 编辑教师
     * @param teacher
     * @return
     */
    @RequestMapping("/teacher/edit")
    public String update(Teacher teacher) {
        teacherService.edit(teacher);
        return "redirect:/toTeacher";
    }

    /**
     * 删除教师,并删除其对应的登录用户表信息
     * @param id
     * @return
     */
    @RequestMapping("/teacher/delete")
    public String delete(String id) {
        //1.删除教师表数据
        teacherService.delete(id);

        //2.删除教师登录表数据
        userService.deleteByUsernameAndUserType(id,Constant.USER_TYPE_TEACHER);

        return "redirect:/toTeacher";
    }

    /**
     * 个人信息修改页面
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/teacher/toPersonal")
    public String toPersonal(Model model,HttpSession session) {
        Teacher sessionStudent = (Teacher) session.getAttribute("teacher");
        Teacher teacher =new Teacher();
    	teacher.setTeacherId(sessionStudent.getTeacherId());
    	teacher = teacherService.findOne(teacher);
    	model.addAttribute("teacher", teacher);
        return "teacher/personal";
    }

    /**
     * 个人信息修改
     * @param teacher
     * @return
     */
    @ResponseBody
    @RequestMapping("/teacher/editPassword")
    public R editPassword(Teacher teacher) {
        teacherService.edit(teacher);
        return R.ok();
    }
    
}
